// pages/Statistic/Statistic.js
const app = getApp();
const db = wx.cloud.database();
var pageHeight = wx.getSystemInfoSync()['windowHeight'];
var rpxRate = 750 / wx.getSystemInfoSync()['windowWidth'];

var avatar = "";
var nickname = "";
var diary = [];
var statisticData = [
    {
        "text": "愉悦",
        "emoji": "../../images/laughing.png",
        "num": 0,
        "color": "rgba(255, 166, 56, 1);",
        "ani": ""
    },
    {
        "text": "轻松",
        "emoji": "../../images/smile.png",
        "num": 0,
        "color": "rgba(251, 238, 105, 1);",
        "ani": ""
    },
    {
        "text": "无语",
        "emoji": "../../images/sighing.png",
        "num": 0,
        "color": "rgba(173, 219, 137, 1);",
        "ani": ""
    },
    {
        "text": "发愁",
        "emoji": "../../images/crying.png",
        "num": 0,
        "color": "rgba(154, 168, 203, 1);",
        "ani": ""
    },
    {
        "text": "崩溃",
        "emoji": "../../images/angry.png",
        "num": 0,
        "color": "rgba(164, 156, 150, 1);",
        "ani": ""
    }
];
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: "",
        nickName: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
            avatar = data.avatar;
            nickname = data.nickname;
            that.getStatistic();
            that.setData({
                avatarUrl: avatar,
                nickName: nickname
            });
        });
    },
    getStatistic(){
        let that = this;
        db.collection("diaryDB").where({
            _openid: app.globalData.openid
        }).get({
            success: function(res){
                for ( let i = 0; i < 5; i++ ) {
                    statisticData[i]["num"] = 0;
                }
                diary = res.data;
                for ( let i in res.data ) {
                    for ( let j = 0; j < 5; j++ ) {
                        if ( res.data[i].emoji == statisticData[j]["text"] ) {
                            statisticData[j]["num"] += 1;
                        }
                    }
                }
                that.statisticLineMove();
                that.setData({
                    signupDay: res.data.length,
                    statisticData: statisticData
                });
            }
        })
    },
    statisticLineMove(){
        let numArr = [];
        for ( let i = 0; i < 5; i++ ) {
            numArr.push(statisticData[i]["num"]);
        }
        numArr.sort();
        let middle = numArr[2];
        for ( let i = 0; i < 5; i++ ) {
            let height = Math.floor(250 * (statisticData[i]["num"] / middle));
            if ( height > 500 )
                height = 500;
            else if ( height < 50 )
                height = 50;
            let heightRPX = height + "rpx";
            let animation = wx.createAnimation({
                duration: 2000,
                timingFunction: 'ease',
            });
            animation.height(heightRPX).step();
            statisticData[i]["ani"] = animation.export();
        }
        
        this.setData({
            statisticData: statisticData
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.statisticLineMove();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})